package com.example.an.mymusic;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;
import java.util.jar.Manifest;

public class MainActivity extends Activity {
    private static final String TAG="MainActivity";


    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private static final int PERMISSON_REQUESTCODE=1;
    private ImageView bingIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        //bingIv=(ImageView)findViewById(R.id.welcome_bing_iv);
        initPermission();
    }

   /* private void loadBinPic(){

    }*/

    private void checkSkip(){
        Timer timer=new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                startMusicActivity();
            }
        };
        timer.schedule(task,3600);
    }

    private void startMusicActivity() {
        Intent intent=new Intent();
        intent.setClass(this,HomeActivity.class);
        startActivity(intent);
        finish();
    }
    private void initPermission(){
        //"Build.VERSION_CODES.M"代表的是Android 6.0的版本
        if(Build.VERSION.SDK_INT<Build.VERSION_CODES.M){
                checkSkip();
            return;
        }
        //(1)ContextCompat.checkSelfPermission()
        // 检查应用是否拥有该权限，被授权返回值为PERMISSION_GRANTED，否则返回PERMISSION_DENIED
        //(2)ActivityCompat.requestPermissions()
        // 将弹出请求授权对话框，这个方法在M之前版本调用，OnRequestPermissionsResultCallback 直接被调用，带着正确的 PERMISSION_GRANTED或者 PERMISSION_DENIED 。
        //(3).AppCompatActivity.onRequestPermissionsResult()
        // 方法类似于Activity的OnActivityResult()的回调方法，主要接收请求授权的返回值

        if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},PERMISSON_REQUESTCODE);
        }else{
            checkSkip();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case PERMISSON_REQUESTCODE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    checkSkip();
                } else {
                    Toast.makeText(this, "必须同意所有权限才能使用本程序", Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
            default:
        }
    }
}
